package mth.rmclusterer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class RunNeighborhood {
	/*
	 * Mit dieser Klasse werden Nachbarschaften einer ermittelten masterImp Lösung bzw. eines
	 * VotedRunResults verwaltet. 
	 * Die Member sind die einzelnen Nachbarschaften mit ihren jeweiligen zugehörigen Objekten.
	 */
	//one member == one handled file of neighborhood data
	private RunNeighborhoodMember[] member;
	private String filename;
	
	RunNeighborhood(String file) {
		filename = file;
		member=new RunNeighborhoodMember[0];
		BufferedReader lineRider;
		try {
			lineRider = new BufferedReader(new FileReader(Manager.PATH_TO_NEIGHBORHOODS+filename));
			String element=lineRider.readLine();
			while (element!=null) {
				createMember(element);
				element = lineRider.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void createMember(String rawData) {
		if (member==null || member.length==0) {
			member = new RunNeighborhoodMember[1];
			member[0] = new RunNeighborhoodMember();
			for (int a=0; a<rawData.split(",").length; a++) {
				member[0].addMember(Integer.parseInt(rawData.split(",")[a]));
			}
		} else {
			RunNeighborhoodMember[] helper = new RunNeighborhoodMember[member.length+1];
			for (int a=0; a<member.length; a++) {
				helper[a]=member[a];
			}
			helper[member.length]=new RunNeighborhoodMember();
			for (int a=0; a<rawData.split(",").length; a++) {
				helper[member.length].addMember(Integer.parseInt(rawData.split(",")[a]));
			}
			member = new RunNeighborhoodMember[helper.length];
			for (int a=0; a<helper.length; a++) {
				member[a]=helper[a];
			}
		}
	}
	
	public RunNeighborhoodMember[] getMember() {
		return member;
	}
}
